﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VirtualPress.Infrastructure.Entities;
using VirtualPress.Infrastructure.Entities.Repository;
using VirtualPress.Tool.Framework;

namespace VirtualPress.Infrastructure.HitMonitor
{
    public class MeetingHitmonitor : BaseMonitor
    {
        public MeetingHitmonitor(object key) : base(key) { }
        internal static string GetMemoryKey(object key)
        {
            return string.Format("HitMonitor_of_Meeting_id:{0}", key);
        }
        protected override void LoadAction()
        {
            if (key != null)
            {
                using (Context ctx = new Context())
                {
                    int id = Int32.Parse(key.ToString());
                    var time = (from c in ctx.Meeting
                                where c.Id == id
                                select new { c.DownloadNumber, c.VisitNumber }).FirstOrDefault();
                    if (time != null)
                    {
                        this.OriginalValue = new MeetingHitPack()
                        {
                            DownloadNumber = time.DownloadNumber,
                            VisitNumber = time.VisitNumber
                        };
                        this.CurrentValue = new MeetingHitPack()
                        {
                            DownloadNumber = time.DownloadNumber,
                            VisitNumber = time.VisitNumber
                        };
                    }
                }
            }
        }

        internal override void Flush()
        {
            if (OriginalValue != null && CurrentValue != null)
            {
                var currentObject = CurrentValue as MeetingHitPack;
                var originalObject = OriginalValue as MeetingHitPack;
                if (currentObject.DownloadNumber != originalObject.DownloadNumber ||
                    currentObject.VisitNumber != originalObject.VisitNumber)
                {
                    using (Context ctx = new Context())
                    {
                        try
                        {
                            ctx.Update<T_Meeting>()
                                .Set(c => c.DownloadNumber, currentObject.DownloadNumber)
                                .Set(c => c.VisitNumber, currentObject.VisitNumber)
                                .WhereSet(c => c.Id, WhereOperator.Equal, key)
                                .End()
                                .Excute();
                            originalObject.DownloadNumber = currentObject.DownloadNumber;
                            originalObject.VisitNumber = currentObject.VisitNumber;
                        }
                        catch (Exception e)
                        {
                            LogHelper.Write(CommonLogger.ExServer, LogLevel.Error, e);
                        }
                    }
                }
            }
        }

        public override void Trigger(int action, int up = 1)
        {
            if (CurrentValue == null)
            {
                LoadProperties();
            }
            if (this.CurrentValue != null)
            {
                var currentObject = CurrentValue as MeetingHitPack;
                var actionName = (MeetingGetAction)action;
                if (actionName == MeetingGetAction.download)
                {
                    currentObject.DownloadNumber += up;
                }
                else if (actionName == MeetingGetAction.visit)
                {
                    currentObject.VisitNumber += up;
                }
            }
        }

        public override void Dispose()
        {
            Flush();
        }
    }

    public class MeetingHitPack
    {
        public int DownloadNumber;
        public int VisitNumber;
    }
    public enum MeetingGetAction
    {
        visit = 1,
        download = 2
    }
}
